<?php
/*

file version 
*/
class downloader {
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
	/*function () {
		
	}*/
/*------------------------------------------------------------------------------
                                                                  public methods
------------------------------------------------------------------------------*/
	/*
	*/
	function downloadString($p=array()) {
		$p['string']=isset($p['string'])?$p['string']:'';
		$p['name']=isset($p['name'])?$p['name']:'download.txt';
		
		if (!$this->sendString($p['string'],$p['name'])) return false;
		exit;
	}
	/* force download of file to client
	*/
	function downloadFile($p=array()) {
		if (!isset($p['file'])) {
			trigger_error("grabber->downloadFile: missing file parameter");
			return false;
		}
		$p['path']=isset($p['path'])?$p['path']:'files/';
		$p['name']=isset($p['name'])?$p['name']:NULL;
		if (!$this->sendFile($p['file'],$p['path'],$p['name'])) {
			trigger_error("grabber->downloadFile: failed to send file {$p['file']}");
			return false;
		}
		exit;
	}
/*------------------------------------------------------------------------------
                                                                      properties
------------------------------------------------------------------------------*/
	//var //
/*------------------------------------------------------------------------------
                                                                 private methods
------------------------------------------------------------------------------*/
	/*
	*/
	function sendString($string,$name) {
		// taille pour les chaines en encodage sur 1 octet
		//$size=strlen($string)*8;
		$size=NULL;
		$this->sendHeaders($name,$size);
		print $string;
		return true;
	}
	
	/* force download file to client
	*/
	function sendFile($file, $path=NULL,$name=NULL) {
		// test de lecture
		if (!is_readable($path.$file)) {
			trigger_error("pas d'acces en lecture sur $path$file",E_USER_WARNING);
			return false;
		}
		// envoi des en-tetes
		$size=filesize($path.$file);
		if ($name===NULL) $name=$file;
		$this->sendHeaders($name,$size);

		// ouverture du fichier
		if (!$fd= fopen($path.$file, 'rb')) {
			trigger_error("pas d'acces en lecture sur $path$file",E_USER_WARNING);
			return false;
			}
		//fpassthru($fd);
		// cette methode de decoupage en parties permet de traiter de gros fichiers
		while(!feof($fd)) {
			$buffer = fread($fd, 2048);
			print $buffer;
		}
		fclose ($fd);
		return true;
	}
	
	function sendHeaders($name,$size=NULL) {
		if (isset($_SERVER['HTTP_USER_AGENT']) and $_SERVER['HTTP_USER_AGENT']=="MSIE") // internet explorer
			header("Content-type: application/force-download");
		else // un bon navigateur ;o)
			header("Content-type: application/octet-stream");
		if ($size)
			header("Content-Length: ".$size);
		header("Content-Disposition: attachment; filename=".$name);
		return true;
	}
}
?>
